package com.example.demo4.controller;

import com.example.demo4.entity.Book;
import com.example.demo4.entity.RespBean;
import com.example.demo4.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/testBoot")
public class BookController {
    @Autowired
    private BookService bookService;
    @RequestMapping(value = "getBook/{id}")
    public RespBean GetBook(@PathVariable int id){
        //return bookService.getBookInfo(id).toString();
        return RespBean.ok("获取书籍成功",bookService.getBookInfo(id));
    }
    @RequestMapping(value = "deleteBook",method = RequestMethod.POST)
    public String DeleteBook(int id){
        int result = bookService.deleteById(id);
        if(id >= 1){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }
    @RequestMapping(value = "updateBook",method = RequestMethod.POST)
    public String UpdateBook(Book book){
        int result = bookService.Update(book);
        if (result >= 1){
            return "修改成功";
        }else {
            return "修改失败";
        }
    }
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public Book insertBook(Book book){
        return bookService.save(book);
    }
    @RequestMapping(value = "/getList",method = RequestMethod.POST)
    public List<Book> ListBook(){
        return bookService.selectAll();
    }

}
